from datetime import datetime
from uuid import uuid4

from django.db import models


# Create your models here.
def upload_img_cover(instance, filename):
    ext = filename.split('.')[-1]
    # 日期目录和 随机文件名
    filename = '{}.{}'.format(uuid4().hex, ext)
    print(filename)
    year = datetime.now().year
    month = datetime.now().month
    day = datetime.now().day
    # instance 可使用instance.user.id
    return "static/upload/{0}/{1}/{2}/{3}".format(year, month, day, filename)


class User(models.Model):
    ## models.CharField 数据库 varchar 表的设置 max_length 必须设置，其它设置可选
    username = models.CharField(max_length=20, null=False, verbose_name="用户名", db_column="name")
    password = models.CharField(max_length=20, verbose_name="密码")
    # 头像
    img = models.ImageField(verbose_name='头像', upload_to=upload_img_cover)

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'book_model'  # 数据的表名


class Xhello(models.Model):
    name = models.CharField(max_length=10)
    birth = models.DateTimeField()
